//
//  GWStatuseLabel.m
//  新浪微博
//
//  Created by qianfeng on 16/2/23.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWStatuseLabel.h"
#import "GWLink.h"
#define GWLinkBackgoundTag 1000
@interface GWStatuseLabel ()
@property(nonatomic,weak)UITextView *textView;
@property (nonatomic,copy)NSMutableArray *links;

@end
@implementation GWStatuseLabel
-(NSMutableArray *)links
{
    
    if (_links ==nil) {
        NSMutableArray *links =[NSMutableArray array];
        
        [_attributedString enumerateAttributesInRange:NSMakeRange(0, _attributedString.length) options:0 usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
            
            NSString *linkText =attrs[GWStatuseLink];
            if (linkText==nil)return ;
            GWLink *link= [[GWLink alloc]init];
            link.linkText =linkText;
            link.linkRange=range;
            NSMutableArray *rectArray =[NSMutableArray array];
            self.textView.selectedRange  =range;
            NSArray *array = [self.textView selectionRectsForRange:self.textView.selectedTextRange];
            for (UITextSelectionRect *rect  in array) {
                
                if (rect.rect.size.width==0||rect.rect.size.height==0) continue;
                [rectArray addObject:rect];
            }
            link.linkRects =rectArray;
            
            
            [links addObject:link];
            
        }];
        
        _links =links;
    }

    return _links;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
        self.backgroundColor =[UIColor clearColor];
        UITextView *textView =[[UITextView alloc]init];
        textView.backgroundColor =[UIColor clearColor];
        textView.editable =NO;
        textView.scrollEnabled =NO;
        textView.userInteractionEnabled =NO;
        textView.textContainerInset =UIEdgeInsetsMake(0, -5, 0, -5);
        [self addSubview:textView];
        self.textView =textView;
        
    }
    return self;
}
-(GWLink *)linkWithPoint:(CGPoint )point
{
    
   __block GWLink *selectedLink =nil;
    [self.links enumerateObjectsUsingBlock:^(GWLink *link, NSUInteger idx, BOOL *stop) {
        
        for (UITextSelectionRect *rect  in link.linkRects) {
            if (CGRectContainsPoint(rect.rect, point)) {
                
                selectedLink =link;
            }
        }
        
    }];
    return selectedLink;
}

-(void)showLinkBackgroundWithLink:(GWLink *)link
{
    for (UITextSelectionRect *rect  in link.linkRects) {
        UIView *view =[[UIView alloc]init];
        view.backgroundColor =[UIColor colorWithRed:1.000 green:0.136 blue:0.119 alpha:0.800];
        view.frame =rect.rect;
        view.tag =GWLinkBackgoundTag;
        view.layer.cornerRadius =3;
        [self insertSubview:view atIndex:0];
    }
}
-(void)removeAllLinkBackground
{
    for (UIView *view in self.subviews) {
        if (view.tag ==GWLinkBackgoundTag) {
            [view removeFromSuperview];
        }
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    UITouch *touch =[touches anyObject];
    CGPoint point =[touch locationInView:self];
    
    GWLink *link =[self linkWithPoint:point];
    
    [self showLinkBackgroundWithLink:link];
    
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch =[touches anyObject];
    CGPoint point =[touch locationInView:self];
    
    GWLink *link =[self linkWithPoint:point];
    if (link) {
        [[NSNotificationCenter defaultCenter]postNotificationName:GWLinkDidSelectedNotification object:nil userInfo:@{GWStatuseLink:link}];
    }
    [self touchesCancelled:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self removeAllLinkBackground];
    });
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    self.textView.frame =self.bounds;
}
-(void)setAttributedString:(NSAttributedString *)attributedString
{
    _attributedString =attributedString;
    self.links =nil;
    self.textView.attributedText =attributedString;
}
@end
